package com.qdlc.p2p.dal.model;

import org.springframework.beans.BeanUtils;

import com.qdlc.p2p.common.exception.ManagerException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.common.util.ValidateUtil;
import com.qdlc.p2p.common.util.code.MD5;
import com.qdlc.p2p.dal.dto.Operator;

/**
 * 管理员Model
 * 
 * @author zhangyz
 * @version 1.0
 * @since 2014-04-03
 */
public class OperatorModel extends Operator {

	/** 原密码 **/
	private String oldPassword;

	/** 确认新修改密码 **/
	private String confirmPassword;

	/**
	 * 实体转换model
	 * 
	 * @param user 实体
	 * @return model
	 */
	public static OperatorModel instance(Operator manager) {
		OperatorModel userModel = new OperatorModel();
		BeanUtils.copyProperties(manager, userModel);
		return userModel;
	}

	/**
	 * model转换实体
	 * 
	 * @return 实体
	 */
	public Operator prototype() {
		Operator manager = new Operator();
		BeanUtils.copyProperties(this, manager);
		return manager;
	}

	/**
	 * 登录 检查提交的数据格式
	 * 
	 * @return
	 */
	public int validLoginModel() {
		if (StringUtil.isBlank(getUserName())) {
			throw new ManagerException("用户名不能为空！", 1);
		}
		if (StringUtil.isBlank(getPwd())) {
			throw new ManagerException("密码不能为空！", 1);
		}
		return -1;
	}

	/**
	 * 修改密码时校验 用户名
	 * @param operatorUserName
	 */
	public void validModifyPwdUsername(String operatorUserName){
		String username = getUserName();
		if(StringUtil.isBlank(username)){
			throw new ManagerException("用户名不能为空！", 1);
		}
		if(!username.equals(operatorUserName)){
			throw new ManagerException("非法的用户名！", 1);
		}
	}
	
	/**
	 * 验证用户登录密码
	 * 
	 * @param user 用户
	 * @return
	 */
	public String validModifyPwdModel(Operator manager) {
		if (getOldPassword() == null) {
			throw new ManagerException("请输入您的原密码！", 1);
		} else if (!MD5.encode(getOldPassword()).equals(manager.getPwd())) {
			throw new ManagerException("原密码错误！", 1);
		} else if (getPwd().equals(getOldPassword())) {
			throw new ManagerException("新密码不能和原密码相同！", 1);
		} else if (!getPwd().equals(getConfirmPassword())) {
			throw new ManagerException("新密码和确认密码不相同！", 1);
		}
		//校验密码长度及组成
		String checkPwd = ValidateUtil.isPwd(getPwd());
		if(StringUtil.isNotBlank(checkPwd)){
			throw new ManagerException(checkPwd,1);
		}
		
		return "";
	}

	/**
	 * 添加用户 检查提交的数据格式
	 * 
	 * @return
	 */
	public int validRegModel() {
		if (!ValidateUtil.isUserName(getUserName())) {
			throw new ManagerException("用户名格式错误！", 1);
		}
		if (StringUtil.isBlank(getPwd())) {
			throw new ManagerException("密码不能为空！", 1);
		}
		if (StringUtil.isBlank(getConfirmPassword())) {
			throw new ManagerException("确认密码不能为空！");
		}
		
		if (!getPwd().equals(getConfirmPassword())) {
			throw new ManagerException("两次输入的密码不一致！");
		}
		return -1;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
}
